package com.weishi.project.sport.mine.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.enums.TypeEnum;
import com.weishi.project.sport.mine.service.MineService;
import com.weishi.project.sport.mine.vo.BodyFunctionLineChartVO;
import com.weishi.project.sport.mine.vo.MineStateDetailsVO;
import com.weishi.project.sport.mine.vo.ValueTimeVO;
import com.weishi.project.sport.trainingdata.vo.TypeValueVO;
import com.weishi.project.sport.vo.PersonalStatusVO;
import com.weishi.project.sport.vo.SportTrainingVO;
import com.weishi.project.system.domain.SysDictData;
import com.weishi.project.system.service.ISysDictDataService;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 个人中心模块 接口实现类
 */
@Service
public class MineServiceImpl implements MineService {


    /**
     * 运动员表 接口
     */
    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 运动员请假表 接口
     */
    @Autowired
    private ILeaveFormService leaveFormService;

    /**
     * 身体机能表 接口
     */
    @Autowired
    private ISportsmanBodyFunctionService sportsmanBodyFunctionService;

    /**
     * 比赛信息表 接口
     */
    @Autowired
    private ISportsmanMatchService sportsmanMatchService;

    /**
     * 训练成绩表 接口
     */
    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    /**
     * 运动员身体形态 接口
     */
    @Autowired
    private ISportsmanBodyShapeService sportsmanBodyShapeService;

    /**
     * 字典数据 子表接口
     */
    @Autowired
    private ISysDictDataService dictDataService;


    /**
     * 查询运动员个人基本信息--( 比赛次数，和 累计训练次数 )
     */
    @Override
    public AjaxResult getMineDetails(Integer userId) {

        // 封装（ 个人中心数据 ，1，运动员用户的个人信息。2，比赛次数。3，累计训练总次数 ）
        SportTrainingVO sportTrainingVO = new SportTrainingVO();

        // 1，运动员用户的个人信息
        sportTrainingVO.setSportsman(sportsmanService.getOne(Wrappers.<Sportsman>lambdaQuery().eq(Sportsman::getId , userId)));

        // 2，比赛次数
        sportTrainingVO.setNumberCompetitions(sportsmanMatchService.list(Wrappers.<SportsmanMatch>lambdaQuery().eq(SportsmanMatch::getSportsmanId , userId)).size());

        // 3，累计训练总次数
        sportTrainingVO.setNumberTraining(trainingAchievementService.list(Wrappers.<TrainingAchievement>lambdaQuery().eq(TrainingAchievement::getSportsmanId , userId)).size());

        return AjaxResult.success("查询成功！" , sportTrainingVO);
    }


    /**
     * 查询运动员个人状态
     */
    @Override
    public AjaxResult getMineState(Integer userId) {

        // 封装个人状态 （ 1、伤病 2、病假 3、事假 4、过敏 5、外训 ）
        PersonalStatusVO personalStatusVO = new PersonalStatusVO();

        // 1，查询伤病总次数
        personalStatusVO.setInjury(leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery().eq(LeaveForm::getSportsmanId , userId).eq(LeaveForm::getType , TypeEnum.INJURY.getCode())).size());

        // 2，查询病假总次数
        personalStatusVO.setSickLeave(leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery().eq(LeaveForm::getSportsmanId , userId).eq(LeaveForm::getType , TypeEnum.SICK_LEAVE.getCode())).size());

        // 3，查询事假总次数
        personalStatusVO.setPersonalLeave(leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery().eq(LeaveForm::getSportsmanId , userId).eq(LeaveForm::getType , TypeEnum.PERSONAL_LEAVE.getCode())).size());

        // 4，查询事假总次数
        personalStatusVO.setAllergy(leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery().eq(LeaveForm::getSportsmanId , userId).eq(LeaveForm::getType , TypeEnum.ALLERGY.getCode())).size());

        // 5，查询事假总次数
        personalStatusVO.setTraining(leaveFormService.list(Wrappers.<LeaveForm>lambdaQuery().eq(LeaveForm::getSportsmanId , userId).eq(LeaveForm::getType , TypeEnum.TRAINING.getCode())).size());

        // 6, 身体机能数据最近更新时间
        List<SportsmanBodyFunction> sportsmanBodyFunctionList = sportsmanBodyFunctionService.list(Wrappers.<SportsmanBodyFunction>lambdaQuery().eq(SportsmanBodyFunction::getSportsmanId , userId).orderByDesc(SportsmanBodyFunction::getCtime));
        if(sportsmanBodyFunctionList.size() > 0){
            personalStatusVO.setBodyFunctionTime(sportsmanBodyFunctionList.get(0).getCtime());
        }

        // 7, 身体形态最近更新时间
        List<SportsmanBodyShape> sportsmanBodyShapeList =  sportsmanBodyShapeService.list(Wrappers.<SportsmanBodyShape>lambdaQuery().eq(SportsmanBodyShape::getSportsmanId , userId).orderByDesc(SportsmanBodyShape::getCtime));
        if(sportsmanBodyShapeList.size() > 0){
            personalStatusVO.setBodyShape(sportsmanBodyShapeList.get(0).getCtime());
        }

        return AjaxResult.success(personalStatusVO);
    }


    /**
     * 查询运动员个人身体机能数据
     */
    @Override
    public AjaxResult getMineBodyFunction(Integer userId) {
        if(sportsmanBodyFunctionService.list(Wrappers.<SportsmanBodyFunction>lambdaQuery().eq(SportsmanBodyFunction::getSportsmanId , userId).orderByDesc(SportsmanBodyFunction::getCtime)).size() > 0){
            return AjaxResult.success("查询成功！" , sportsmanBodyFunctionService.list(Wrappers.<SportsmanBodyFunction>lambdaQuery().eq(SportsmanBodyFunction::getSportsmanId , userId).orderByDesc(SportsmanBodyFunction::getCtime)).get(0));
        }else{
            return AjaxResult.success("暂无数据！" , 0);
        }
    }


    /**
     * 查询运动员个人身体形态数据详情 -- 折线图
     */
    @Override
    public AjaxResult getMineStateDetails(Integer userId) {
        List<MineStateDetailsVO> mineStateDetailsVOS = new ArrayList<>();
        List<SportsmanBodyShape> sportsmanBodyShape = sportsmanBodyShapeService.list(Wrappers.<SportsmanBodyShape>lambdaQuery().eq(SportsmanBodyShape::getSportsmanId , userId).orderByDesc(SportsmanBodyShape::getCtime));
        dictDataService.getTypeValue("body_shape").forEach( p-> {
            MineStateDetailsVO mineStateDetailsVO = new MineStateDetailsVO();
            mineStateDetailsVO.setNames(p.getDictLabel());
            mineStateDetailsVO.setTypes(p.getDictValue());

            // 身高
            if(p.getDictValue().equals("height")){
                List<ValueTimeVO> height = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getHeight());
                    valueTimeVO.setTimes(pp.getCtime());
                    height.add(valueTimeVO);
                    mineStateDetailsVO.setValues(height);
                });

                // 坐高
            }else if(p.getDictValue().equals("sitHigh")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getSitHigh());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 臂展
            }else if(p.getDictValue().equals("armSpread")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getArmSpread());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 肩宽
            }else if(p.getDictValue().equals("shoulderWidth")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getShoulderWidth());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 腰围
            }else if(p.getDictValue().equals("waist")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getWaistline());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 臀围
            }else if(p.getDictValue().equals("hipline")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getHipline());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 胸围
            }else if(p.getDictValue().equals("bust")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getBust());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 小腿长
            }else if(p.getDictValue().equals("lowerLeg")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getTinyLegLength());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

                // 腿长
            }else if(p.getDictValue().equals("legLength")){
                List<ValueTimeVO> sitHigh = new ArrayList<>();
                sportsmanBodyShape.forEach( pp-> {
                    ValueTimeVO valueTimeVO = new ValueTimeVO();
                    valueTimeVO.setValue(pp.getLegLength());
                    valueTimeVO.setTimes(pp.getCtime());
                    sitHigh.add(valueTimeVO);
                    mineStateDetailsVO.setValues(sitHigh);
                });

            }

            mineStateDetailsVOS.add(mineStateDetailsVO);
        });

        return AjaxResult.success(mineStateDetailsVOS);
    }


    /**
     * 查询运动员个人物理属性列表
     */
    @Override
    public AjaxResult getBodyFunction(Integer userId) {

        return AjaxResult.success(sportsmanBodyFunctionService.list(Wrappers.<SportsmanBodyFunction>lambdaQuery().eq(SportsmanBodyFunction::getSportsmanId , userId).orderByDesc(SportsmanBodyFunction::getCtime)));
    }


    /**
     * 运动员个人信息修改
     */
    @Override
    public AjaxResult updateById(Integer userId , Sportsman sportsman) {
        sportsman.setId(userId);
        sportsmanService.updateById(sportsman);
        return AjaxResult.success();
    }


    /**
     * 运动员个人 -- 新增身体形态数据
     */
    @Override
    public AjaxResult save(Integer userId, SportsmanBodyShape sportsmanBodyShape) {
        sportsmanBodyShape.setSportsmanId(Long.parseLong(userId.toString()));
        sportsmanBodyShape.setCtime(new Date());
        sportsmanBodyShapeService.save(sportsmanBodyShape);
        return AjaxResult.success();
    }


    /**
     * 运动员个人 -- 查询身体形态字典子表数据
     */
    @Override
    public AjaxResult getDictData() {
        return AjaxResult.success(dictDataService.getTypeValue("body_shape"));
    }



    /**
     * 运动员个人 -- 查询身体机能---折线图
     */
    @Override
    public AjaxResult getBodyFunctionLineChart(Integer userId) {
        List<MineStateDetailsVO> bodyFunctionVOList = new ArrayList<>();
        List<SportsmanBodyFunction> sportsmanBodyFunctionList = sportsmanBodyFunctionService.list(Wrappers.<SportsmanBodyFunction>lambdaQuery().eq(SportsmanBodyFunction::getSportsmanId , userId).orderByDesc(SportsmanBodyFunction::getCtime));
        List<TypeValueVO> sysDictDataList = dictDataService.getTypeValue("body_function");
        if(sysDictDataList.size() > 0){
            sysDictDataList.forEach( p-> {
                MineStateDetailsVO bodyFunctionLineChartVO = new MineStateDetailsVO();
                bodyFunctionLineChartVO.setNames(p.getDictLabel());
                bodyFunctionLineChartVO.setTypes(p.getDictValue());

                // 血红蛋白
                if(p.getDictValue().equals("HB")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getHb());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 血尿素氮
                }else if(p.getDictValue().equals("BU")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getBu());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 肌酸激酶
                }else if(p.getDictValue().equals("CK")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getCk());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 血压高压
                }else if(p.getDictValue().equals("HBP")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getHbp());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 尿酸
                }else if(p.getDictValue().equals("UA")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getUa());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 晨脉
                }else if(p.getDictValue().equals("HR")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getHr());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 血清睾酮
                }else if(p.getDictValue().equals("ST")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getSt());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 血乳酸
                }else if(p.getDictValue().equals("BLA")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getBla());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 体重
                }else if(p.getDictValue().equals("Weight")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getWeight());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                    // 血压低压
                }else if(p.getDictValue().equals("LBP")){
                    List<ValueTimeVO> HB = new ArrayList<>();
                    sportsmanBodyFunctionList.forEach( pp-> {
                        ValueTimeVO valueTimeVO = new ValueTimeVO();
                        valueTimeVO.setValue(pp.getLbp());
                        valueTimeVO.setTimes(pp.getCtime());
                        HB.add(valueTimeVO);
                        bodyFunctionLineChartVO.setValues(HB);
                    });

                }else{

                }

                bodyFunctionVOList.add(bodyFunctionLineChartVO);
            });
            return AjaxResult.success(bodyFunctionVOList);
        }else{
            return AjaxResult.success("暂无数据！");
        }
    }








}
